﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;
using Video.Helper;
namespace Video.User
{
    public class UserController : Controller
    {
        #region 注册 +RegistSub(string eName, string Email, string Pwd)
        public ActionResult Regist()
        {
            return View();
        }
        public JsonResult RegistSub(string eName, string Email, string Pwd)
        {
            //后台验证数据正确性
            if (true)
            {
                if (OperateContext.Current.RegistResult(eName, Email, Pwd))
                {
                    return OperateContext.Current.RedirectAjax("ok", "注册成功~", null, "/Home/Index");
                }
            }
            return OperateContext.Current.RedirectAjax("err", "注册失败~", null, "/User/Regist");
        }
        #endregion

        #region 用户登录 和维持登录状态
        public ActionResult LoginStatus()
        {
            //定义当前用户信息
            MODEL.user CurrentUser;
            //判断session和cookie中用户是否有保存信息 有则返回model 没有则返回null
            CurrentUser = OperateContext.Current.IsLogin();
            if (CurrentUser != null)
            {
                ViewData["UserInfo"] = CurrentUser;
                return View();
            }
            else
            {
                return View();
            }
        }
        public ActionResult LoginInfo(string Email, string Pwd,string IsAlway)
        {
            if (OperateContext.Current.LoginInfoCheck(Email, Pwd, IsAlway)==null) {
                 OperateContext.Current.RedirectAjax("err", "用户名或密码错误", null, "");
            }
            LoginStatus();
            return OperateContext.Current.RedirectAjax("ok","登录成功",null,"/Home/Index");
        }
        //登出
        public ActionResult LoginOut()
        {
            OperateContext.Current.UserSession = null;
            LoginStatus();
            return OperateContext.Current.RedirectAjax("ok", "注销成功", null, "/Home/Index");
        } 
        #endregion

        public ActionResult Index()
        {
            return View();
        }
    }
}
